<?php

namespace common\helpers;

use Yii;
use yii\helpers\BaseFormatConverter;

/**
 * FormatConverter provides functionality to convert between different formatting pattern formats.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FormatConverter extends BaseFormatConverter {

    /**
     * 输出xml微信需要的字符串
     * 问候一句微信开发你麻痹还自定义xml返回，草
     * 
     * @param array $data 待格式化的内容
     * @return string
     */
    public static function toWechatXml($data) {
        if (!is_array($data) || empty($data)) {
            return '';
        }
        $xml = "<xml>";
        foreach ($data as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    /**
     * 数字格式化成汉字
     * 
     * @param number $figure 待格式化的数字
     * @param boolean $capital 使用汉字大写数字
     * @param boolean $mode 单字符转换模式
     * @return string
     */
    public static function toChineseNumerals($figure, $capital = false, $mode = true) {
        $figure = str_replace(',', '', $figure);

        $numberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        $unitChar = ['', '十', '百', '千', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
        if ($capital !== false) {
            $numberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            $unitChar = ['', '拾', '佰', '仟', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
        }

        $dec = "点";
        $target = '';
        $matches = [];

        if ($mode) {
            preg_match("/^0*(\d*)\.?(\d*)/", $figure, $matches);
        } else {
            preg_match("/(\d*)\.?(\d*)/", $figure, $matches);
        }

        list(, $number, $point) = $matches;

        if ($point) {
            $target = $dec . self::toChineseNumerals($point, $capital, false);
        }

        if (!$number) {
            return $target;
        }

        $str = strrev($number);
        for ($i = 0; $i < strlen($str); $i++) {
            $out[$i] = $numberChar[$str[$i]];
            if ($mode === false) {
                continue;
            }
            $out[$i] .= $str[$i] != '0' ? $unitChar[$i % 4] : '';
            if ($i > 0 && $str[$i] + $str[$i - 1] == 0) {
                $out[$i] = '';
            }
            if ($i % 4 == 0) {
                $temp = substr($str, $i, 4);
                $out[$i] = str_replace($numberChar[0], '', $out[$i]);
                if (strrev($temp) > 0) {
                    $out[$i] .= $unitChar[4 + floor($i / 4)];
                } else {
                    $out[$i] .= $numberChar[0];
                }
            }
        }
        return join('', array_reverse($out)) . $target;
    }

    /**
     * Html内容中img相对地址批量更换
     */
    public static function appendRemoteDomain($html, $domain = null) {
        $pattern = "/<[img].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png||\.jpeg]))[\'|\"].*?[\/]?>/i";
        return preg_replace_callback($pattern, function($matches) use ($domain) {
            $element = $matches[0];
            $src = $matches[1];
            if (strpos($src, "://") === false) {
                $dst = Url::attach($src, $domain);
                $element = str_replace($src, $dst, $element);
            }
            return preg_replace('/(<img.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1', $element);
        }, $html);
    }

    public static function parseRemoteDomain($html, $domain = null) {
        if ($domain === null) {
            $domain = Yii::getAlias('@attachUrl');
        }
        $pattern = "/<[img].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png||\.jpeg]))[\'|\"].*?[\/]?>/i";
        return preg_replace_callback($pattern, function($matches) use ($domain) {
            $element = $matches[0];
            $src = $matches[1];
            if (strpos($src, "://") !== false) {
                $element = str_replace($domain, '', $element);
            }
            return preg_replace('/(<img.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1', $element);
        }, $html);
    }

}
